package com.quick.im.client.strategy;

import com.quick.im.client.abstracts.AbsMessageStrategy;
import com.quick.im.client.enums.MessageContentTypeServerEnum;
import com.quick.im.client.factory.StrategyBeanFactory;
import com.quick.im.client.interfaces.MessageStrategy;
import com.quick.im.common.entity.message.MessageInfo;
import com.quick.im.common.enums.GlobalExceptionEnum;
import jakarta.websocket.Session;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 一对一消息处理
 */
@Slf4j
@Service
public class PersonMessageStrategy extends AbsMessageStrategy {

    @Setter(onMethod_ = @Autowired)
    private StrategyBeanFactory factory;

    @Override
    public void sendMessage(MessageInfo messageInfo, Session session) throws Exception {

        if (StringUtils.isNotBlank(String.valueOf( messageInfo.getMessageContentType()))) {
            MessageContentTypeServerEnum messageStrategyEnum = MessageContentTypeServerEnum.getServiceById(messageInfo.getMessageContentType());
            if (messageStrategyEnum == null) {
                throw new Exception("MessageContentTypeServerEnum 未找到对应的MessageContentType key值,MessageContentType:"+messageInfo.getMessageContentType());
            }
            MessageStrategy messageServerByName = factory.getMessageServerByName(messageStrategyEnum.getService());
            messageServerByName.onMessage(messageInfo, session);
        }else {
            throw new Exception(GlobalExceptionEnum.MESSAGE_CONTENT_TYPE_NOT_NULL.getCode());

        }
    }

    /**
     * 后续校验是否有消息发送的权限(是否是好友等。。。)
     * @param messageInfo
     * @param session
     * @throws Exception
     */
    @Override
    protected void sendMessageBefore(MessageInfo messageInfo, Session session) throws Exception {

    }

    @Override
    protected void sendMessageAfter(MessageInfo messageInfo, Session session) {
        // 保存消息
    }
}
